package com.cxrj.zhsq.parkingLotManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.parkingLotManage.dao.PropertyParkingRegionMapper;
import com.cxrj.zhsq.parkingLotManage.dao.PropertyParkingSpaceMapper;
import com.cxrj.zhsq.parkingLotManage.entity.PropertyParkingRegion;
import com.cxrj.zhsq.parkingLotManage.entity.PropertyParkingSpace;
import com.cxrj.zhsq.parkingLotManage.service.ParkingRegionService;
@Service
public class ParkingRegionServiceImpl implements ParkingRegionService {
	@Resource
	private PropertyParkingRegionMapper parkingRegionMapper;
	@Resource
	private PropertyParkingSpaceMapper parkingSpaceMapper;

	@Override
	public Map<String, Object> selectParkingRegionByPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		sqlMap.put("name", request.getParameter("name"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = parkingRegionMapper.selectByCount(sqlMap) + "";
		List<PropertyParkingRegion> list = parkingRegionMapper.selectParkingRegionByPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteParkingRegion(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", 0);
		int a=parkingRegionMapper.deleteByPrimaryKey(id);
		parkingSpaceMapper.deleteByRid(id);
		if(a>0){
			resultMap.put("result", a);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectParkingRegionById(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyParkingRegion parkingRegion=parkingRegionMapper.selectByPrimaryKey(id);
		resultMap.put("parkingRegion", parkingRegion);
		return resultMap;
	}

	@Override
	public Map<String, Object> checkingNumber(String number, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("number", number);
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		PropertyParkingRegion parkingRegion=parkingRegionMapper.checkingNumber(sqlMap);
		resultMap.put("result", 0);
		if (parkingRegion==null) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertParkingRegion(PropertyParkingRegion parkingRegion, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", 0);
		parkingRegion.setId(UUID.randomUUID().toString());
		parkingRegion.setCommunityId((String) request.getSession().getAttribute("communityId"));
		parkingRegion.setCommunityName((String)request.getSession().getAttribute("communityName"));
		parkingRegion.setCreateTime(new Date());
		int a=parkingRegionMapper.insertSelective(parkingRegion);
		if (a>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateParkingRegion(PropertyParkingRegion parkingRegion, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", 0);
		int a=parkingRegionMapper.updateByPrimaryKeySelective(parkingRegion);
		if (a>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectParkingChart(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html;charset=utf-8");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		List<PropertyParkingRegion> list = parkingRegionMapper.selectParkingChart(sqlMap);
		if(list.size() > 0) {
			resultMap.put("list", list);
			resultMap.put("result", 1);
		}else {
			resultMap.put("result", 0);
		}
		return resultMap;
	}
}
